﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace CricketHub
{
    public partial class TeamProfile : PhoneApplicationPage
    {
        Teamprofile obj = new Teamprofile();
        public TeamProfile()
        {
            InitializeComponent();
            obj.nw = this;
            obj.Requesting_url();
            teamprofile.Visibility = Visibility.Visible;
        }


        List<countries_data> data = new List<countries_data>();
        public void UpdateList(string countries)
        {
            var element = XElement.Parse(countries);
            var count = element.Descendants("sports").Count();
            System.Diagnostics.Debug.WriteLine("count for sports..." + count);
            for (int i = 0; i < count; i++)
            {
                var nextelement = element.Descendants("sports").ElementAt(i);
                countries_data obj = new countries_data();
                obj.sub_name = nextelement.Element("sub_name").Value;
                obj.hasnode = nextelement.Element("hasNode").Value;
                obj.id = nextelement.Element("id").Value;
                data.Add(obj);
                System.Diagnostics.Debug.WriteLine("values for the xml..subname..........." + obj.sub_name + "id....................." + obj.id);

            }
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                CreateListBox();

            });

        }
        public void CreateListBox()
        {
            LoadListbox();
            UpdateList();
            teamprofile.Visibility = Visibility.Collapsed;
        }
        ListBox mylist;
        public void LoadListbox()
        {
            mylist = new ListBox();
            mylist.Height = 630;
            mylist.Width = 450;
            mylist.HorizontalAlignment = HorizontalAlignment.Stretch;
            mylist.VerticalAlignment = VerticalAlignment.Top;
            mylist.Background = new SolidColorBrush(Colors.Transparent);
            ContentPanel.Children.Add(mylist);
            mylist.SelectionChanged += new SelectionChangedEventHandler(mylist_SelectionChanged);


        }
        private void mylist_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (mylist.SelectedIndex != -1)
            {
                countries_data selectdata = data.ElementAt(mylist.SelectedIndex);
                var country = App.Current as App;
                country.countries = selectdata.sub_name;
                string subname = selectdata.sub_name;
                App cuntry = App.Current as App;
                cuntry.country_selcted = mylist.SelectedIndex;
               // MessageBox.Show(subname);
                mylist.SelectedIndex = -1;
                NavigationService.Navigate(new Uri("/Teams.xaml", UriKind.RelativeOrAbsolute));

            }
        }
        public void UpdateList()
        {
            for (int index = 0; index < data.Count; index++)
            {
                countries_data reterview_data = data.ElementAt(index);
                StackPanel stk = new StackPanel();
                stk.Name = "stack";
                stk.Height = 120;
                stk.HorizontalAlignment = HorizontalAlignment.Left;
                Image img = new Image();
                img.Height = 100;
                img.Width = 100;
                img.Name = "img" + index.ToString();
                img.Margin = new Thickness(0, 10, 0, 0);
                img.Source = new BitmapImage(new Uri(index+".png", UriKind.RelativeOrAbsolute));
                img.HorizontalAlignment = HorizontalAlignment.Left;
                img.VerticalAlignment = VerticalAlignment.Top;
                TextBlock textblk = new TextBlock();
                textblk.Name = "txtblk" + index.ToString();
                textblk.Text = reterview_data.sub_name.ToString();
                textblk.FontSize = 30;
                textblk.Foreground = new SolidColorBrush(Colors.Black);
                textblk.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                textblk.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                textblk.Margin = new Thickness(220, -110, 0, 0);
                Line line = new Line();
                line.X1 = 0;
                line.Y1 = 0;
                line.X2 = 500;
                line.Y2 = 0;
                line.StrokeThickness = 5;

                line.Stroke = new SolidColorBrush(Colors.Green);
                stk.Children.Add(line);
                stk.Children.Add(img);
                stk.Children.Add(textblk);
                mylist.Items.Add(stk);

            }

        }

    }
}